home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / TVTOOLS.ZIP / FILEVIEW.CPP < prev    next >
C/C++ Source or Header  |  1993-03-10  |  5KB  |  184 lines

  1. #define Uses_TFileView
  2. #define Uses_MsgBox
  3. #define Uses_TKeys
  4. #define Uses_TScroller
  5. #define Uses_TDrawBuffer
  6. #define Uses_TRect
  7. #define Uses_TProgram
  8. #define Uses_TDeskTop
  9. #define Uses_TStreamableClass
  10. #include "tvtools.h"
  11. __link(RScroller)
  12. __link(RScrollBar)
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18.  
  19. #include <fstream.h>
  20.  
  21.  
  22. const char * const TFileViewer::name = "TFileViewer";
  23.  
  24. TFileViewer::TFileViewer( const TRect& bounds,
  25.                           TScrollBar *aHScrollBar,
  26.                           TScrollBar *aVScrollBar,
  27.                           const char *aFileName) :
  28.     TScroller( bounds, aHScrollBar, aVScrollBar )
  29. {
  30.     growMode = gfGrowHiX | gfGrowHiY;
  31.     isValid = True;
  32.     fileName = 0;
  33.     readFile( aFileName );
  34. }
  35.  
  36. TFileViewer::~TFileViewer()
  37. {
  38.      delete fileName;
  39.      destroy (fileLines);
  40. }
  41.  
  42. void TFileViewer::draw()
  43. {
  44.     char *p;
  45.  
  46.     ushort c =  getColor(0x0301);
  47.     for( int i = 0; i < size.y; i++ )
  48.         {
  49.         TDrawBuffer b;
  50.     b.moveChar( 0, ' ', c, size.x );
  51.  
  52.         if( delta.y + i < fileLines->getCount() )
  53.             {
  54.             char s[maxLineLength+1];
  55.             p = (char *)( fileLines->at(delta.y+i) );
  56.             if( p == 0 || strlen(p) < delta.x )
  57.                 s[0] = EOS;
  58.             else
  59.         {
  60.         strncpy( s, p+delta.x, size.x );
  61.         if( strlen( p + delta.x ) > size.x )
  62.                     s[size.x] = EOS;
  63.                 }
  64.             b.moveStr( 0, s, c );
  65.             }
  66.         writeBuf( 0, i, size.x, 1, b );
  67.         }
  68. }
  69.  
  70. void TFileViewer::scrollDraw()
  71. {
  72.     TScroller::scrollDraw();
  73.     draw();
  74. }
  75.  
  76. void TFileViewer::readFile( const char *fName )
  77. {
  78.     delete fileName;
  79.  
  80.     limit.x = 0;
  81.     fileName = newStr( fName );
  82.     fileLines = new TLineCollection(5, 5);
  83.     ifstream fileToView( fName );
  84.     if( !fileToView )
  85.         {
  86.         messageBox( "Invalid drive or directory", mfError | mfOKButton );
  87.         isValid = False;
  88.         }
  89.     else
  90.         {
  91.         char line[maxLineLength+1];
  92.         while( !lowMemory() &&
  93.                !fileToView.eof() && 
  94.                fileToView.get( line, sizeof line ) != 0 
  95.              )
  96.             {
  97.             char c;
  98.             fileToView.get(c);      // grab trailing newline
  99.             limit.x = max( limit.x, strlen( line ) );
  100.             fileLines->insert( newStr( line ) );
  101.             }
  102.         isValid = True;
  103.         }
  104.     limit.y = fileLines->getCount();
  105. }
  106.  
  107. void TFileViewer::setState( ushort aState, Boolean enable )
  108. {
  109.     TScroller::setState( aState, enable );
  110.     if( enable && (aState & sfExposed) )
  111.         setLimit( limit.x, limit.y );
  112. }
  113.  
  114. Boolean TFileViewer::valid( ushort )
  115. {
  116.     return isValid;
  117. }
  118.  
  119. void *TFileViewer::read(ipstream& is)
  120. {
  121.     char *fName;
  122.  
  123.     TScroller::read(is);
  124.     fName = is.readString();
  125.     fileName = 0;
  126.     readFile(fName);
  127.     delete fName; 
  128.     return this;
  129. }
  130.  
  131. void TFileViewer::write(opstream& os)
  132. {
  133.     TScroller::write(os);
  134.     os.writeString(fileName);
  135. }
  136.  
  137. TStreamable *TFileViewer::build()
  138. {
  139.     return new TFileViewer( streamableInit );
  140. }
  141.  
  142.  
  143. TStreamableClass RFileView( TFileViewer::name,
  144.                             TFileViewer::build,
  145.                               __DELTA(TFileViewer)
  146.                           );
  147.  
  148.  
  149.  
  150. static int winNumber = 0;
  151.  
  152. TFileWindow::TFileWindow( const char *fileName ) :
  153.     TWindow( TProgram::deskTop->getExtent(), fileName, winNumber++ ),
  154.     TWindowInit( &TFileWindow::initFrame )
  155. {
  156.   growTo( size.x / 2, size.y / 2 );
  157.   options |= ofTileable | ofCentered;
  158.   TRect r( getExtent() );
  159.   r.grow(-1, -1);
  160.   insert(new TFileViewer( r,
  161.                           standardScrollBar(sbHorizontal | sbHandleKeyboard),
  162.                           standardScrollBar(sbVertical | sbHandleKeyboard),
  163.                           fileName) );
  164. }
  165.  
  166.  
  167. TFileViewDialog::TFileViewDialog( const char *fileName ) :
  168.     TDialog( TProgram::deskTop->getExtent(), fileName ),
  169.     TWindowInit( &TFileViewDialog::initFrame )
  170. {
  171.   flags = wfMove | wfGrow | wfClose | wfZoom;
  172.   options |= ofTileable | ofCentered;
  173.   growMode = gfGrowAll | gfGrowRel;
  174.   growTo( size.x / 2, size.y / 2 );
  175.   TRect r( getExtent() );
  176.   r.grow(-1, -1);
  177.   insert(new TFileViewer( r,
  178.                           standardScrollBar(sbHorizontal | sbHandleKeyboard),
  179.                           standardScrollBar(sbVertical | sbHandleKeyboard),
  180.                           fileName) );
  181. }
  182.  
  183.  
  184.